package io.renren.modules.pet.controller;

import io.renren.common.page.PageData;
import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.pet.vo.PetAdoptionVO;
import io.renren.modules.pet.dto.PetAdoptionDTO;
import io.renren.modules.pet.excel.PetAdoptionExcel;
import io.renren.modules.pet.service.PetAdoptionService;
import io.renren.modules.pet.vo.PetSendVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 宠物领养申请表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-15
 */
@RestController
@RequestMapping("pet/petadoption")
@Api(tags="宠物领养申请表")
public class PetAdoptionController {
    @Autowired
    private PetAdoptionService petAdoptionService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<PetAdoptionVO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PetAdoptionVO> page = petAdoptionService.page(params);
        return new Result<PageData<PetAdoptionVO>>().ok(page);
    }

    @GetMapping("getMyAdoptionPets")
    @ApiOperation("获取当前用户领养的宠物")
    public Result<List<PetSendVO>> getMyAdoptionPets(@ApiIgnore @RequestParam Map<String, Object> params){
        List<PetSendVO> list = petAdoptionService.getMyAdoptionPets(params);
        return new Result<List<PetSendVO>>().ok(list);
    }

    @GetMapping("getByPetSendId/{petSendId}")
    @ApiOperation("根据送养宠物ID来获取宠物领养申请表信息")
    public Result<List<PetAdoptionVO>> getByPetSendId(@PathVariable("petSendId") Long petSendId){
        List<PetAdoptionVO> list = petAdoptionService.getByPetSendId(petSendId);
        return new Result<List<PetAdoptionVO>>().ok(list);
    }

    @PostMapping("agreePetAdoption")
    @ApiOperation("同意领养")
    @LogOperation("同意领养")
    public Result<Map<String, Object>> agreePetAdoption(@RequestBody PetAdoptionDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        petAdoptionService.agreePetAdoption(dto);
        return new Result<Map<String, Object>>();
    }

    @PostMapping("disagreePetAdoption")
    @ApiOperation("不同意领养")
    @LogOperation("不同意领养")
    public Result<Map<String, Object>> disagreePetAdoption(@RequestBody PetAdoptionDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        petAdoptionService.disagreePetAdoption(dto);
        return new Result<Map<String, Object>>();
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<PetAdoptionVO> get(@PathVariable("id") Long id){
        PetAdoptionVO data = petAdoptionService.get(id);
        return new Result<PetAdoptionVO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result<Map<String, Object>> savePetAdoption(@RequestBody PetAdoptionDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        petAdoptionService.savePetAdoption(dto);
        return new Result<Map<String, Object>>();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result<Map<String, Object>> update(@RequestBody PetAdoptionDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        petAdoptionService.update(dto);
        return new Result<Map<String, Object>>();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result<Map<String, Object>> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        petAdoptionService.delete(ids);
        return new Result<Map<String, Object>>();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<PetAdoptionVO> list = petAdoptionService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, list, PetAdoptionExcel.class);
    }

}